SlideShare a Scribd company logo
1 of 55
Class No.15  Data Structures http://ecomputernotes.com
Level-order Traversal ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Level-order Traversal Level-order: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal Queue: 14 Output: 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 4 15 Output: 14 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 15 3 9 Output: 14 4 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 3 9 18 Output: 14 4 15 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 9 18 Output: 14 4 15 3  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 18 7 Output: 14 4 15 3 9  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 7 16 20 Output: 14 4 15 3 9 18  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 16 20 5 Output: 14 4 15 3 9 18 7  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 20 5 17 Output: 14 4 15 3 9 18 7 16  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 5 17 Output: 14 4 15 3 9 18 7 16 20  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 17 Output: 14 4 15 3 9 18 7 16 20 5  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: Output: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Storing other Type of Data ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info  << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );  }  http://ecomputernotes.com
Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info  << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );  }  http://ecomputernotes.com
Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info  << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );  }  http://ecomputernotes.com
Binary Search Tree with Strings ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket ,[object Object],http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket ,[object Object],[object Object],http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket ,[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket ,[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket ,[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Deleting a node in BST ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Deleting a node in BST ,[object Object],6 2 4 3 1 8 http://ecomputernotes.com
Deleting a node in BST ,[object Object],6 2 4 3 1 8 6 2 4 3 1 8  http://ecomputernotes.com
Deleting a node in BST ,[object Object],6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8   http://ecomputernotes.com
Deleting a node in BST ,[object Object],[object Object],http://ecomputernotes.com
Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder  successor http://ecomputernotes.com
Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder  successor ,[object Object],[object Object],http://ecomputernotes.com
Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4 http://ecomputernotes.com

More Related Content

Similar to computer notes - Data Structures - 15

computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
ecomputernotes
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
Jevgeni Kabanov
 
Shkrubbel for Open Web Camp 3
Shkrubbel for Open Web Camp 3Shkrubbel for Open Web Camp 3
Shkrubbel for Open Web Camp 3
kitthod
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slides
Jorge Joens
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
ecomputernotes
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 

Similar to computer notes - Data Structures - 15 (20)

computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
 
Computer notes - Recursive
Computer notes  - RecursiveComputer notes  - Recursive
Computer notes - Recursive
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Php Sq Lite
Php Sq LitePhp Sq Lite
Php Sq Lite
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
State Machines to State of the Art
State Machines to State of the ArtState Machines to State of the Art
State Machines to State of the Art
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Oscon 2010 Specs talk
Oscon 2010 Specs talkOscon 2010 Specs talk
Oscon 2010 Specs talk
 
Arduino section programming slides
Arduino section programming slidesArduino section programming slides
Arduino section programming slides
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
 
(Slightly) Smarter Smart Pointers
(Slightly) Smarter Smart Pointers(Slightly) Smarter Smart Pointers
(Slightly) Smarter Smart Pointers
 
Groovy
GroovyGroovy
Groovy
 
#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx
 
Shkrubbel for Open Web Camp 3
Shkrubbel for Open Web Camp 3Shkrubbel for Open Web Camp 3
Shkrubbel for Open Web Camp 3
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slides
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
MCE^3 - Hannes Verlinde - Let The Symbols Do The Work
MCE^3 - Hannes Verlinde - Let The Symbols Do The WorkMCE^3 - Hannes Verlinde - Let The Symbols Do The Work
MCE^3 - Hannes Verlinde - Let The Symbols Do The Work
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Computer notes - Hashing
Computer notes - HashingComputer notes - Hashing
Computer notes - Hashing
 

More from ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
ecomputernotes
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
ecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
ecomputernotes
 

More from ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
 

Recently uploaded

obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
yulianti213969
 
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
Boundify
 

Recently uploaded (20)

Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
How does a bike-share company navigate speedy success? - Cyclistic
How does a bike-share company navigate speedy success? - CyclisticHow does a bike-share company navigate speedy success? - Cyclistic
How does a bike-share company navigate speedy success? - Cyclistic
 
Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business Potential
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptx
 
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptxGoal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
 
A DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptxA DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptx
 
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
 
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptxThompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
 
The Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdf
The Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdfThe Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdf
The Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdf
 
WheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond InsightsWheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond Insights
 
Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024
 
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
 
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptxQSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 

computer notes - Data Structures - 15

  • 1. Class No.15 Data Structures http://ecomputernotes.com
  • 2.
  • 3. Level-order Traversal Level-order: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 4.
  • 5. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 6. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 7. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 8. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 9. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 10. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 11. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 12. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 13. Level-order Traversal Queue: 14 Output: 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 14. Level-order Traversal Queue: 4 15 Output: 14 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 15. Level-order Traversal Queue: 15 3 9 Output: 14 4 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 16. Level-order Traversal Queue: 3 9 18 Output: 14 4 15 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 17. Level-order Traversal Queue: 9 18 Output: 14 4 15 3 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 18. Level-order Traversal Queue: 18 7 Output: 14 4 15 3 9 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 19. Level-order Traversal Queue: 7 16 20 Output: 14 4 15 3 9 18 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 20. Level-order Traversal Queue: 16 20 5 Output: 14 4 15 3 9 18 7 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 21. Level-order Traversal Queue: 20 5 17 Output: 14 4 15 3 9 18 7 16 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 22. Level-order Traversal Queue: 5 17 Output: 14 4 15 3 9 18 7 16 20 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 23. Level-order Traversal Queue: 17 Output: 14 4 15 3 9 18 7 16 20 5 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 24. Level-order Traversal Queue: Output: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 25.
  • 26. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 27. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 28. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 29. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 30. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 31. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 32. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 33. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 34. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 35. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 36. Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }  http://ecomputernotes.com
  • 37. Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }  http://ecomputernotes.com
  • 38. Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }  http://ecomputernotes.com
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor http://ecomputernotes.com
  • 52.
  • 53. Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 54. Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 55. Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4 http://ecomputernotes.com

Editor's Notes

  1. End of lecture 14
  2. Start lecture 16
  3. End of lecture 15